from contextlib import asynccontextmanager
from redis import asyncio as aioredis
from redis.asyncio.connection import ConnectionPool
from fastapi import FastAPI
from fastapi import Request


@asynccontextmanager
async def lifespan(app: FastAPI):
    await startup_event(app)
    yield
    await shutdown_event(app)


async def startup_event(app):
    pool = ConnectionPool(port=6380, max_connections=20, decode_responses=True)
    app.state.redis_client = aioredis.Redis(connection_pool=pool)
    await app.state.redis_client.flushall()
    await app.state.redis_client.set("test_key", 'testdata')
    print(await app.state.redis_client.get("test_key"))
    await app.state.redis_client.set("test_zh", '我是谁')
    print(await app.state.redis_client.get("test_zh"))


async def shutdown_event(app):
    print('11341')
    app.state.redis_client.close()


app = FastAPI(lifespan=lifespan)


@app.get("/index")
async def index():
    key = 'xiaozhong'
    # 设置缓存数据
    await app.state.redis_client.set(name=key, value="测试数据")
    # 读取缓存数据
    cache1 = await app.state.redis_client.get(key)

    key_2 = 'xiaozhong_2'
    # 添加数据，5秒后自动清除
    await app.state.redis_client.setex(key_2, time=5, value="测试数据2")
    # 测试2缓存数据的获取
    cache2 = await app.state.redis_client.get(key_2)
    return {
        "cache1": cache1,
        "cache2": cache2,
    }


@app.get("/index2")
async def index2(request: Request):
    async with request.app.state.redis_client.pipeline(transaction=True) as pipe:
        ok1, ok2 = await (pipe.set("xiaozhong", "测试数据").set("xiaozhong_2", "测试数据2").execute())
        print(ok1, ok2)
    async with request.app.state.redis_client.pipeline(transaction=True) as pipe:
        cache1, cache2 = await (pipe.get("xiaozhong").get("xiaozhong_2").execute())
        print(cache1, cache2)
    return {
        "cache1": cache1,
        "cache2": cache2,
    }


if __name__ == "__main__":
    import uvicorn
    import os

    app_model_name = os.path.basename(__file__).replace(".py", "")
    print(app_model_name)
    uvicorn.run(f"{app_model_name}:app", host='127.0.0.1')
